package main

import (
	"fmt"
	"sync"
)

type Server struct {
	OnlineMap map[string]*User
	mapLock   sync.RWMutex
	Groups    map[string]*Group
	groupLock sync.RWMutex
}

func NewServer() *Server {
	return &Server{
		OnlineMap: make(map[string]*User),
		Groups:    make(map[string]*Group),
	}
}

func (s *Server) Broadcast(user *User, msg string) {
	finalMsg := fmt.Sprintf("[%s]: %s", user.Name, msg)
	s.mapLock.RLock()
	for _, u := range s.OnlineMap {
		u.SendMsg(finalMsg)
	}
	s.mapLock.RUnlock()
}
